﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SharedGenomics.Annotations;
using SharedGenomics.Workbench.Annotations;
using SharedGenomics.Workbench.Tests.TestObjects;

using Rhino.Mocks;

namespace SharedGenomics.Workbench.Tests.Annotations
{
    [TestClass]
    public class RepositoryUpdateAnnotationRequestStageTests
    {
        private MockRepository mocks;
        private IAnnotationRepositoryMediator annotations;
        private RepositoryUpdateAnnotationRequestStage stage;

        [TestInitialize]
        public void Setup()
        {
            this.mocks = new MockRepository();
            this.annotations = this.mocks.StrictMock<IAnnotationRepositoryMediator>();
            this.stage = new RepositoryUpdateAnnotationRequestStage(this.annotations);
        }

        [TestMethod]
        public void ShouldAddToRepository()
        {
            var annotation = new PubMedSearchAnnotation(1234);
            AnnotationValue parentValue = new AnnotationValue(AnnotationKey.EntrezGeneId, 1234);

            using (this.mocks.Record())
            {
                this.annotations.Expect(a => a.Add(AnnotationKey.PubMedSearch, annotation, parentValue));
            }

            using (this.mocks.Playback())
            {
                var context = new AnnotationRequestContext(AnnotationKey.PubMedSearch, "rs001");
                context.AddAnnotation(annotation, parentValue);

                var traversalStrategy = new TestAnnotationRequestPipelineTraversalStrategy();
                this.stage.Process(context, traversalStrategy);

                Assert.IsTrue(traversalStrategy.MovedNext);
            }
        }

        [TestMethod]
        public void ShouldNotAddToRepositoryIfRequestFailed()
        {
            this.mocks.ReplayAll();

            using (this.mocks.Playback())
            {
                var context = new AnnotationRequestContext(AnnotationKey.PubMedSearch, "rs001");
                context.FailRequest();
                var traversalStrategy = new TestAnnotationRequestPipelineTraversalStrategy();
                this.stage.Process(context, traversalStrategy);

                Assert.IsTrue(traversalStrategy.MovedNext);
            }
        }
    }
}
